<?php

require_once 'Vega/Cache/Output.php';

function cache_manager(Vega_Cache_Output $manager = null)
{
    static $staticManager;
    
    if (null != $manager)
    {
        $staticManager = $manager;
    }
    return $staticManager;
}

function cache_start($blockName, $lifetime, $prefix = null)
{
    $cache = cache_manager(null);

    if (null === $cache)
    {
        return false;
    }
    
    if ($output = $cache->load($blockName . '_block'))
    {
        echo $output;
        return true;
    }
    
    $expired = true;
    cache_registry($blockName, $lifetime, $prefix, $expired);
    ob_start();
    return false;
}

function cache_registry($name, $lifetime = null, $prefix = null, $expired = false)
{
    static $blocks = array();
    
    if (null !== $lifetime)
    {
        $blocks[$name] = array('lifetime' => $lifetime, 'prefix' => $prefix, 'expired' => $expired);
    }
    else
    {
        if (!isset($blocks[$name]))
        {
            return false;
        }
        return $blocks[$name];
    }
}

function cache_stop($blockName)
{    
    $info = cache_registry($blockName);

    if (!$info && (isset($info['expired']) || $info['expired'] == false))
    {
        // the cached block was out
        return;
    }
    
    $cache = Vega_Cache_Output::getInstance();
    
    $blockOutput = ob_get_clean();
    
    $cache->save($blockName . '_block', $blockOutput, $info['lifetime'], $info['prefix']);
}




